package com.qilu.pe.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.qilu.pe.R;
import com.qilu.pe.base.Config;
import com.qilu.pe.ui.activity.ConfirmExpertExplainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：ikkong （ikkong@163.com），修改 jeasonlzy（廖子尧）
 * 版    本：1.0
 * 创建日期：2016/5/19
 * 描    述：
 * 修订历史：微信图片选择的Adapter, 感谢 ikkong 的提交
 * ================================================
 */
public class ImagePickerAdapter extends RecyclerView.Adapter<ImagePickerAdapter.SelectedPicViewHolder> {
    private int maxImgCount;
    private Context mContext;
    private List<ImageItem> mData;
    private List<String> mExistData=new ArrayList<>();
    private LayoutInflater mInflater;
    private OnRecyclerViewItemClickListener listener;
    private boolean isAdded;   //是否额外添加了最后一个图片
    private boolean isExist=false;
    private int status;

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.listener = listener;
    }

    public void setImages(List<ImageItem> data) {
        mData = new ArrayList<>(data);
        if (getItemCount() < maxImgCount) {
            mData.add(new ImageItem());
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataSetChanged();
    }
    public void setExistImages(List<String> data,boolean isExist) {
        mExistData = new ArrayList<>(data);
//        this.status=status;
        this.isExist=isExist;
        if (isExist) {
//            if(status==2){
//                mExistData.add(new String());
//            }
            isAdded = true;
        } else {
            isAdded = false;
        }
        notifyDataSetChanged();
    }
    public List<ImageItem> getImages() {
        //由于图片未选满时，最后一张显示添加图片，因此这个方法返回真正的已选图片
        if (isAdded) return new ArrayList<>(mData.subList(0, mData.size() - 1));
        else return mData;
    }

    public ImagePickerAdapter(Context mContext, List<ImageItem> data, int maxImgCount) {
        this.mContext = mContext;
        this.maxImgCount = maxImgCount;
        this.mInflater = LayoutInflater.from(mContext);
        setImages(data);
    }

    @Override
    public SelectedPicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new SelectedPicViewHolder(mInflater.inflate(R.layout.list_item_image, parent, false));
    }

    @Override
    public void onBindViewHolder(SelectedPicViewHolder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if(mExistData.size()>0){
            return mExistData.size();
        }else{
            return mData.size();
        }
    }

    public class SelectedPicViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView iv_img;
        private int clickPosition;

        public SelectedPicViewHolder(View itemView) {
            super(itemView);
            iv_img = (ImageView) itemView.findViewById(R.id.iv_img);
        }

        public void bind(int position) {
            //设置条目的点击事件
            itemView.setOnClickListener(this);
            //根据条目位置设置图片
            if(!isExist){
                ImageItem item = mData.get(position);
                if (isAdded && position == getItemCount() - 1) {
                    iv_img.setImageResource(R.mipmap.report_detail_confirm_expert_add_img);
                    clickPosition = ConfirmExpertExplainActivity.IMAGE_ITEM_ADD;
                } else {
                    ImagePicker.getInstance().getImageLoader().displayImage((Activity) mContext, item.uri, iv_img, 0, 0);
                    clickPosition = position;
                }
            }else{
                String urlPath=mExistData.get(position);
//                if (isAdded && position == getItemCount() - 1&&status==2) {
//                    iv_img.setImageResource(R.mipmap.report_detail_confirm_expert_add_img);
//                    clickPosition = ConfirmExpertExplainActivity.IMAGE_ITEM_ADD;
//                } else {
                    Glide.with(mContext).load(urlPath).into(iv_img);
                    clickPosition = position;
//                }
            }

        }

        @Override
        public void onClick(View v) {
            if (listener != null) listener.onItemClick(v, clickPosition);
        }
    }
}